package debugServer;

import YAPserver.gameObjects.Mod;
import YAPserver.gameObjects.Point;
import YAPserver.logic.AbstractModel;
import YAPserver.logic.Game;
import YAPserver.logic.GameModifier;
import YAPserver.mods.*;

public class DebugConsole {

	public static void addMod(GlobalModCode code, Game game){
		GameModifier mod = game.getGameModel().getGameModifier();
		switch((GlobalModCode) code){
		case doubleBall:
			addDoubleBall(mod);
			return;
		case eightBall:
			addEightBall(mod);
			return;
		case nineBall:
			addNineBall(mod);
			return;
		case hugeBall:
			addHugeBall(mod);
			return;
		case rotate:
			addRotate(mod);
			return;
		case randomBounce:
			addRandomBounce(mod);
		default:
			return;
		}
	}

	public static void activate(GlobalModCode code, Game game) {
		GameModifier mod = game.getGameModel().getGameModifier();
		switch((GlobalModCode) code){
		case doubleBall:
			activateDoubleBall(mod);
			return;
		case eightBall:
			activateEightBall(mod);
			return;
		case nineBall:
			activateNineBall(mod);
			return;
		case hugeBall:
			activateHugeBall(mod);
			return;
		case rotate:
			activateRotate(mod);
			return;
		case randomBounce:
			activateRandomBounce(mod);
			return;
		default:
			return;
		}
	}
	
	private static void addRandomBounce(GameModifier mod) {
		mod.addModMoving(new Mod(new Point(300, 300), Math.random()*Math.PI*2, 3, 5, mod, new ModWallRandomBounce()));
	}

	private static void addRotate(GameModifier mod) {
		mod.addModMoving(new Mod(new Point(300, 300), Math.random()*Math.PI*2, 3, 5, mod, new ModRotate()));
	}

	private static void addHugeBall(GameModifier mod) {
		mod.addModMoving(new Mod(new Point(300, 300), Math.random()*Math.PI*2, 3, 5, mod, new ModHugeBall()));
	}

	private static void addNineBall(GameModifier mod) {
		mod.addModMoving(new Mod(new Point(300, 300), Math.random()*Math.PI*2, 3, 5, mod, new ModNineBall()));
	}
	
	private static void addEightBall(GameModifier mod){
		mod.addModMoving(new Mod(new Point(300, 300), Math.random()*Math.PI*2, 3, 5, mod, new ModEightBall()));
	}

	private static void addDoubleBall(GameModifier mod) {
		mod.addModMoving(new Mod(new Point(300, 300), Math.random()*Math.PI*2, 3, 5, mod, new ModDoubleBall()));
	}
	
	private static void activateRandomBounce(GameModifier mod) {
		
		mod.activateMod(new ModWallRandomBounce());
	}

	private static void activateRotate(GameModifier mod) {
		mod.activateMod(new ModRotate());
	}

	private static void activateHugeBall(GameModifier mod) {
		mod.activateMod(new ModHugeBall());
	}

	private static void activateNineBall(GameModifier mod) {
		mod.activateMod(new ModNineBall());
	}
	
	private static void activateEightBall(GameModifier mod){
		mod.activateMod(new ModEightBall());
	}

	private static void activateDoubleBall(GameModifier mod) {
		mod.activateMod(new ModDoubleBall());
	}

	public static void activateMod(LocalModCode selection, Game game, int id) {
		GameModifier mod = game.getGameModel().getGameModifier();
		switch(selection){
		case bouncePlayer:
			activateBouncePlayer(mod, id);
			return;
		case safetyNet:
			activateSafetyNet(mod, id);
			return;
		case sliderDouble:
			activateSliderDouble(mod, id);
			return;
		case sliderHalf:
			activateSliderHalf(mod, id);
			return;
		case topsyTurvy:
			activateTopsyTurvy(mod, id);
			return;
		default:
			return;
		}
	}

	private static void activateTopsyTurvy(GameModifier mod, int id) {
		mod.activateMod(id, new ModTopsyTurvy());
	}

	private static void activateSliderHalf(GameModifier mod, int id) {
		mod.activateMod(id, new ModSldLengthHalf());
	}

	private static void activateSliderDouble(GameModifier mod, int id) {
		mod.activateMod(id, new ModSldLengthDouble());
	}

	private static void activateSafetyNet(GameModifier mod, int id) {
		mod.activateMod(id, new ModSafetyNet());
	}

	private static void activateBouncePlayer(GameModifier mod, int id) {
		mod.activateMod(id, new ModBounceToPlayer());
	}

	public static void addMod(LocalModCode selection, Game game, int i) {
		GameModifier mod = game.getGameModel().getGameModifier();
		switch(selection){
		case bouncePlayer:
			addBouncePlayer(mod);
			return;
		case safetyNet:
			addSafetyNet(mod);
			return;
		case sliderDouble:
			addSliderDouble(mod);
			return;
		case sliderHalf:
			addSliderHalf(mod);
			return;
		case topsyTurvy:
			addTopsyTurvy(mod);
			return;
		default:
			return;
		}
	}

	private static void addTopsyTurvy(GameModifier mod) {
		mod.addModMoving(new Mod(new Point(300, 300), Math.random()*Math.PI*2, 3, 5, mod, new ModTopsyTurvy()));
	}

	private static void addSliderHalf(GameModifier mod) {
		mod.addModMoving(new Mod(new Point(300, 300), Math.random()*Math.PI*2, 3, 5, mod, new ModSldLengthHalf()));
	}

	private static void addSliderDouble(GameModifier mod) {
		mod.addModMoving(new Mod(new Point(300, 300), Math.random()*Math.PI*2, 3, 5, mod, new ModSldLengthDouble()));
	}

	private static void addSafetyNet(GameModifier mod) {
		mod.addModMoving(new Mod(new Point(300, 300), Math.random()*Math.PI*2, 3, 5, mod, new ModSafetyNet()));
	}

	private static void addBouncePlayer(GameModifier mod) {
		mod.addModMoving(new Mod(new Point(300, 300), Math.random()*Math.PI*2, 3, 5, mod, new ModBounceToPlayer()));
	}

	public static void perform(MiscCode selection, Game game, double arg, int gameId) {
		GameModifier mod = game.getGameModel().getGameModifier();
		switch(selection){
		case setPlayerLife:
			setPlayerLife(mod, (int) Math.round(arg), gameId);
			return;
		case setAllPlayerLife:
			setAllPlayerLife(mod, (int) Math.round(arg));
			return;
		case setAcceleration:
			setAccelaration(mod, arg);
			return;
		case setNumberOfMods:
			setNumberOfMods(mod, (int) Math.round(arg));
			return;
		case slowDown:
			slowDown(mod, arg);
		case debug:
			debug(mod);
		default:
			return;
		}
	}

	private static void debug(GameModifier mod) {
		//stop creating mods
		if(mod.getGamePlay().getGameModel() instanceof AbstractModel){
			((AbstractModel) mod.getGamePlay().getGameModel()).setNumberOfMods(0);
		}
		//give players more lives
		mod.setAllPlayerLives(100);
		//set zero acceleration
		if(mod.getGamePlay().getGameModel() instanceof AbstractModel){
			((AbstractModel) mod.getGamePlay().getGameModel()).setBallIncrementSpeed(0);
			((AbstractModel) mod.getGamePlay().getGameModel()).setModIncrementSpeed(0);
		}
		//TODO replace with debug method in standardModel
	}

	private static void slowDown(GameModifier mod, double arg) {
		mod.setAllBallSpeed(arg);
		mod.setAllModSpeed(arg);
		mod.setBallDefaultSpeed(arg);
		mod.setModDefaultSpeed(arg);
	}

	private static void setNumberOfMods(GameModifier mod, int arg) {
		if(mod.getGamePlay().getGameModel() instanceof AbstractModel){
			((AbstractModel) mod.getGamePlay().getGameModel()).setNumberOfMods(arg);
		}
	}

	private static void setAccelaration(GameModifier mod, double arg) {
		if(mod.getGamePlay().getGameModel() instanceof AbstractModel){
			((AbstractModel) mod.getGamePlay().getGameModel()).setBallIncrementSpeed(arg);
			((AbstractModel) mod.getGamePlay().getGameModel()).setModIncrementSpeed(arg);
		}
	}

	private static void setAllPlayerLife(GameModifier mod, int lives) {
		mod.setAllPlayerLives(lives);
	}

	private static void setPlayerLife(GameModifier mod, int lives, int gameId) {
		mod.setPlayerLives(gameId, lives);
	}
	
}
